//
//  UIView+Category.m
//  VICategory
//
//  Created by lan's Mac on 2022/7/20.
//

#import "UIView+Category.h"

@implementation UIView (Category)
- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (void)removeAllSubviews{
    for (id view in self.subviews) {
        [view removeFromSuperview];
    }
}


- (void)vi_setShadowPathWith:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowSide:(VIShadowPathSide)shadowPathSide shadowPathWidth:(CGFloat)shadowPathWidth {
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOpacity = shadowOpacity;
    self.layer.shadowRadius =  shadowRadius;
    self.layer.shadowOffset = CGSizeZero;
    CGRect shadowRect;
    CGFloat originW = self.bounds.size.width;
    CGFloat originH = self.bounds.size.height;
    switch (shadowPathSide) {
        case VIShadowPathTop:
            shadowRect  = CGRectMake(0, - shadowPathWidth/2, originW,  shadowPathWidth);
            break;
        case VIShadowPathBottom:
            shadowRect  = CGRectMake(0, originH -shadowPathWidth/2, originW, shadowPathWidth);
            break;
        case VIShadowPathLeft:
            shadowRect  = CGRectMake(-shadowPathWidth/2, 0, shadowPathWidth, originH);
            break;
        case VIShadowPathRight:
            shadowRect  = CGRectMake(originW - shadowPathWidth/2, 0, shadowPathWidth, originH);
            break;
        case VIShadowPathNoTop:
            shadowRect  = CGRectMake(-shadowPathWidth/2, 1, originW +shadowPathWidth,originH + shadowPathWidth/2 );
            break;
        case VIShadowPathAllSide:
            shadowRect  = CGRectMake(-shadowPathWidth/2, - shadowPathWidth/2, originW +  shadowPathWidth, originH + shadowPathWidth);
            break;
       
          }
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
    self.layer.shadowPath = path.CGPath;
}

+ (UIViewController *)vi_findCurrentViewController {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *topViewController = [window rootViewController];
    while (true) {
        if (topViewController.presentedViewController) {
            topViewController = topViewController.presentedViewController;
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            topViewController = [(UINavigationController *)topViewController topViewController];
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
        } else {
            break;
        }
    }
    return topViewController;
}

- (UITableView *)addTable{
    return [self addTableStyle:UITableViewStylePlain];
}

- (UITableView *)addTableStyle:(UITableViewStyle)style{
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectZero style:style];
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.tableFooterView = [UIView new];
    if (@available(iOS 11.0, *)) {
        table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    [self addSubview:table];
    return table;
}

- (UICollectionView *)addCollection{
    return [self addCollectionWithScrollDirection:UICollectionViewScrollDirectionVertical];
}
- (UICollectionView *)addCollectionWithScrollDirection:(UICollectionViewScrollDirection)scrollDirection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = scrollDirection;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                                         collectionViewLayout:layout];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    [self addSubview:collectionView];
    return collectionView;
}

- (UIView *)addWhiteView{
    return [self addViewColor:UIColor.whiteColor];
}
- (UIView *)addDefaultView{
    return [self addViewColor:nil];
}
- (UIView *)addViewColor:(nullable UIColor * )color{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = color;
    [self addSubview:view];
    return view;
}

- (UILabel *)addLabelTextColor:(nullable UIColor  *)textColor fontSize:(CGFloat)fontSize{
    return [self addLabelTextColor:textColor fontSize:fontSize fontWeight:UIFontWeightRegular];
}
- (UILabel *)addLabelTextColor:(nullable UIColor *)textColor
                      fontSize:(CGFloat)fontSize
                    fontWeight:(UIFontWeight)fontWeight{
    UILabel *label = [[UILabel alloc]init];
    if (textColor) label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
    [self addSubview:label];
    return label;
}


- (UIImageView *)addImgV{
    return [self addImgVWithImageName:nil];
}
- (UIImageView *)addImgVWithImage:(UIImage *)image{
    UIImageView *imageView = [[UIImageView alloc]init];
    if (image) imageView.image = image;
    [self addSubview:imageView];
    return imageView;
}
- (UIImageView *)addImgVWithImageName:(nullable NSString *)name{
    UIImageView *imageView = [[UIImageView alloc]init];
    UIImage *image = nil;
    if (name) image = [UIImage imageNamed:name];
    if (image) imageView.image = image;
    [self addSubview:imageView];
    return imageView;
}
- (void)roundCorner:(CGFloat)radius{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

- (UIButton *)addButtonFontSize:(CGFloat)fontSize
                    normalTitle:(nullable NSString *)normalTitle
                    normalColor:(nullable UIColor *)normalColor{
    return [self addButtonFontSize:fontSize
                       normalTitle:normalTitle
                       normalColor:normalColor
                      cornerRadius:0];
}
- (UIButton *)addButtonFontSize:(CGFloat)fontSize
                    normalTitle:(nullable NSString *)normalTitle
                    normalColor:(nullable UIColor *)normalColor
                   cornerRadius:(CGFloat)radius{
    return [self addButtonFontSize:fontSize
                       normalTitle:normalTitle
                       selectTitle:nil
                       normalColor:normalColor
                       selectColor:nil
                       normalImage:nil
                       selectImage:nil
                      cornerRadius:radius];
}
- (UIButton *)addButtonNormalImage:(nullable UIImage *)normalImage{
    return [self addButtonNormalImage:normalImage selectImage:nil];
}
- (UIButton *)addButtonNormalImage:(nullable UIImage *)normalImage
                       normalTitle:(nullable NSString *)normalTitle
                  normalTitleColor:(nullable UIColor *)normalTitleColor{
    return [self addButtonFontSize:0
                       normalTitle:normalTitle
                       selectTitle:nil
                       normalColor:normalTitleColor
                       selectColor:nil
                       normalImage:normalImage
                       selectImage:nil
                      cornerRadius:0];
}
- (UIButton *)addButtonNormalImage:(nullable UIImage *)normalImage
                       selectImage:(nullable UIImage *)selectImage{
    return [self addButtonFontSize:0
                       normalTitle:nil
                       selectTitle:nil
                       normalColor:nil
                       selectColor:nil
                       normalImage:normalImage
                       selectImage:selectImage
                      cornerRadius:0];
}
- (UIButton *)addButtonFontSize:(CGFloat)fontSize
                    normalTitle:(nullable NSString *)normalTitle
                    selectTitle:(nullable NSString *)selectTitle
                    normalColor:(nullable UIColor *)normalColor
                    selectColor:(nullable UIColor *)selectColor
                    normalImage:(nullable UIImage *)normalImage
                    selectImage:(nullable UIImage *)selectImage
                   cornerRadius:(CGFloat)radius{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if(fontSize > 0)button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    
    if(normalTitle) [button setTitle:normalTitle forState:UIControlStateNormal];
    if(selectTitle) [button setTitle:selectTitle forState:UIControlStateSelected];
    
    if(normalColor) [button setTitleColor:normalColor forState:UIControlStateNormal];
    if(selectColor) [button setTitleColor:selectColor forState:UIControlStateSelected];
    
    if(normalImage) [button setImage:normalImage forState:UIControlStateNormal];
    if(selectImage) [button setImage:selectImage forState:UIControlStateSelected];
    
    if(radius > 0)  [button roundCorner:radius];
    
    //iPhone 15 iOS17以上，文字和图片存在  文字显示不全
    if (@available(iOS 17.0, *)) {
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -1);
        button.titleLabel.lineBreakMode = NSLineBreakByClipping;
    }
    [self addSubview:button];
    return button;
}

+ (void)drawDashLine:(UIView *)lineView lineLength:(NSInteger)lineLength lineSpacing:(NSInteger)lineSpacing lineColor:(UIColor *)lineColor {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];

    [shapeLayer setStrokeColor:lineColor.CGColor];

    // 设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];

    // 设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithLong:lineLength], [NSNumber numberWithLong:lineSpacing], nil]];

    // 设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);

    [shapeLayer setPath:path];
    CGPathRelease(path);

    // 把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}
@end
